package com.zb.activemq.have_two_try;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by dycong on 2017/8/15.
 */
public class ExpireQueue {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory;
        Connection connection = null;
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD,
                "tcp://39.108.78.245:61616");
        try {
            connection = connectionFactory.createConnection();
            connection.start();
            Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
            Destination destination = session.createQueue("ActiveMQ.DLQ");
            MessageConsumer consumer = session.createConsumer(destination);
            while (true) {
                ObjectMessage message = (ObjectMessage) consumer.receive(500000);
                if (null != message) {
                    System.out.println("Consumer: " + message.getObject());
                } else {
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != connection)
                    connection.close();
            } catch (Throwable ignore) {
            }
        }
    }
}
/*
当一个消息被redelivered超过maximumRedeliveries(缺省为6次，具体设置请参考后面的链接)次数时，会给broker发送一个"Poison ack"，这个消息被认为是a poison pill，这时broker会将这个消息发送到DLQ，以便后续处理。

缺省情况下，死信队列是ActiveMQ.DLQ，如果没有特别指定，死信都会被发送到这个队列。

缺省情况下，持久消息过期，会被送到DLQ，非持久消息不会送到DLQ

可以通过配置文件(activemq.xml)来调整死信发送策略。

注:本次测试使用的是第三种,使用自行确认接收并且在失败时session.recover()进行恢复.
参考：https://my.oschina.net/thinwonton/blog/997783
 */